<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cetakr extends CI_Controller {

	public function __construct() {
		parent::__construct();
	}

	public function month($bln,$thn){
		switch ($bln) {
                            case 1  : $nambln= "Januari";
                            break;
                            case 2  : $nambln= "Februari";
                            break;
                            case 3  : $nambln= "Maret";
                            break;
                            case 4  : $nambln= "April";
                            break;
                            case 5  : $nambln= "Mei";
                            break;
                            case 6  : $nambln= "Juni";
                            break;
                            case 7  : $nambln= "Juli";
                            break;
                            case 8  : $nambln= "Agustus";
                            break;
                            case 9  : $nambln= "September";
                            break;
                            case 10 : $nambln= "Oktober";
                            break;
                            case 11 : $nambln= "November";
                            break;
                            case 12 : $nambln= "Desember";
                            break;
                        }
		$this->load->library('excel');
		$this->excel->setActiveSheetIndex(0);
		$this->excel->getActiveSheet()->setTitle('laporan');
		$this->excel->getActiveSheet()->setCellValue('A1', "Laporan Bulan ".$nambln." ".$thn);
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->mergeCells('A1:F1');
		$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		for ($col = 'A'; $col != 'G'; $col++) {
			$this->excel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
		}
		$this->excel->getActiveSheet()->getStyle('A2:G2')->getFont()->setBold(true);
		$this->excel->setActiveSheetIndex(0)
            ->setCellValue('A2', 'No Normalisasi')
            ->setCellValue('B2', 'Nama Barang')
            ->setCellValue('C2', 'Sisa Bulan Lalu')
            ->setCellValue('D2', 'Masuk ke Rayon')
            ->setCellValue('E2', 'Kembali ke Area')
            ->setCellValue('F2', 'Sisa Bulan Ini');

        $this->excel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        $tanggal = "$thn-$bln-01";
		$this->load->model('laporan','',true);
		$list = $this->laporan->monthr($tanggal);
		$row = 3;
		foreach($list as $data){
			$this->excel->getActiveSheet()->getStyle("A".$row.":F".$row)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
			$this->excel->setActiveSheetIndex(0)
            ->setCellValue("A".$row, $data['kodebarang'])
            ->setCellValue("B".$row, $data['nama'])
            ->setCellValue("C".$row, $data['awal'])
            ->setCellValue("D".$row, $data['masuk'])
            ->setCellValue("E".$row, $data['keluar'])
            ->setCellValue("F".$row, ($data['awal']+$data['masuk']-$data['keluar']));
            $row++;
		}

		$filename="Laporan Bulan ".$nambln." ".$thn; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
             
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
		
	}
	
}